package com.caigou.modules.service.ui

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.caigou.comm.base.container.BaseFragment
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.ext.observe
import com.caigou.comm.base.ext.showToast
import com.caigou.comm.base.widget.SpaceItemDecoration
import com.caigou.modules.service.R
import com.caigou.modules.service.adapter.PlatformItemAdapter
import com.caigou.modules.service.baen.PlatformItemBean
import com.caigou.modules.service.databinding.FragmentPlatformBinding
import com.caigou.modules.service.viewmodel.ServiceViewModel
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2021/11/10
 */
@AndroidEntryPoint
class PlatformFragment : BaseFragment() {

    private var _binding: FragmentPlatformBinding? = null
    private val binding get() = _binding!!

    private val serviceViewModel: ServiceViewModel by viewModels()

    private var platformList = mutableListOf<PlatformItemBean>()

    private val linearLayoutManager: LinearLayoutManager by lazy {
        LinearLayoutManager(requireActivity())
    }

    private val mAdapter: PlatformItemAdapter by lazy {
        PlatformItemAdapter(mutableListOf())
    }

    private val recyclerViewItemDecoration by lazy {
        SpaceItemDecoration(requireActivity())
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = FragmentPlatformBinding.inflate(inflater, container, false)
        return _binding?.root
    }

    override fun initView() {
        binding.platformSwipeRefreshLayout.run {
            setColorSchemeColors(resources.getColor(R.color.caigou_theme_color))
            setOnRefreshListener {
                serviceViewModel.getPlatformList()
            }
        }

        binding.websiteCollectRecyclerView.run {
            layoutManager = linearLayoutManager
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(recyclerViewItemDecoration)
        }
        mAdapter.setOnItemClickListener { adapter, view, position ->
            openBrowser(platformList[position].applyUrl)
        }

        binding.platformTvBack.setOnClickListener {
            (requireActivity() as ServiceActivity).onBackPressed()
        }
    }

    override fun lazyLoad() {
        serviceViewModel.getPlatformList()
    }

    override fun onBack() {
    }

    override fun observeViewModel() {
        super.observeViewModel()
        observe(serviceViewModel.platformListLiveData, ::handleGetPlatformList)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    private fun handleGetPlatformList(result: Resource<MutableList<PlatformItemBean>>) {
        when (result) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                binding.platformSwipeRefreshLayout.isRefreshing = false
                if (!TextUtils.isEmpty(result.errorMsg)) {
                    showToast(result.errorMsg)
                } else {
                    showToast("获取推荐平台信息异常")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                binding.platformSwipeRefreshLayout.isRefreshing = false

                if (result.data == null) {
                    return
                }
                platformList = result.data!!
                mAdapter.setList(platformList)
            }
        }
    }

    private fun openBrowser(url: String) {
        if (url.startsWith("https://") || url.startsWith("http://")) {
            startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
        } else {
            startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://${url}")).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
        }
    }
}